<?php
require_once('bad_lists.php');

function validate_email($email, $check_dns = false) {
	$isValid = true;
	$atIndex = strrpos($email, '@');
	if (is_bool($atIndex) && !$atIndex) {
		$isValid = false;
	} else {
		$domain = substr($email, $atIndex+1);
		$local = substr($email, 0, $atIndex);
		$localLen = strlen($local);
		$domainLen = strlen($domain);
		if ($localLen < 1 || $localLen > 64) {
			$isValid = false;
		} else if ($domainLen < 1 || $domainLen > 255) {
			$isValid = false;
		} else if ($local[0] == '.' || $local[$localLen-1] == '.') {
			$isValid = false;
		} else if (preg_match('/\\.\\./', $local)) {
			$isValid = false;
		} else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
			$isValid = false;
		} else if (preg_match('/\\.\\./', $domain)) {
			$isValid = false;
		} else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
			if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) {
				$isValid = false;
			}
		}

		if ($isValid && $check_dns && !(checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) ) {
			$isValid = false; 
		}
	}
	return $isValid;
}
function level2_check($str, $reg = false) {
	require('bad_lists.php');
	$badwords_full = array();
	foreach ($bad_words as $word) {
		$badwords_full[] = 'rx!\b'.$word.'\b';
	}
	if (check_string($str, $scam_messages)) 
		return true;
	if (check_string($str, $general_stop))
		return true;
	if ($reg) {
		if (check_string($str, $badwords_full, 1))
			return true;
	}
	if (find_multichars($str))
		return true;

	if ($reg)
		return;

	if (check_string($str, $social_sites))
		return true;

	if (find_phone($str))
		return true;

	if (find_email($str))
		return true;

}

function level3_check($str) {
	require('bad_lists.php');
	$badwords_full = array();
	foreach ($bad_words as $word) {
		$badwords_full[] = 'rx!\b'.$word.'\b';
	}

	if (check_string($str, $badwords_full))
		return true;

	if (find_multichars($str))
		return true;
}


function find_multichars($str) {
	if (preg_match('/(\S)\1{14,}/', $str))
		return true;
}
function find_multiphrases($str) {
	if (preg_match_all('/(.{10,})(?=\1+)/', $str, $matches)) {
		if (empty($matches[1][1]) || (preg_match('/^\s+$/', $matches[1][1])))
			return;
		return true;
	}
}

function find_email($str) {
	if (preg_match('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/i', $str))
		return true;
}

function find_phone($str) {
	if (preg_match('/(([\+]\d{1,3})?[ \.-]?[\(]?\d{3}[\)]?)?[ \.-]?\d{3}[ \.-]?\d{4}/is', $str))
		return true;
}

function find_websites($str) {
	if (preg_match('/https?:\/\//i', $str))
		return true;
}

function ip_to_country($ip) {

	$country = '';
	$answer = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip));
	if (isset($answer['geoplugin_countryCode'])) {
		$country = $answer['geoplugin_countryCode'];
	}

	if (!$country) {
			$country = file_get_contents('http://ipinfo.io/'.$ip.'/country');
	}

	if (!$country) {
		debug("No results from geoip checks");
		return;
	}
	return $country;
}

function country_id_to_code($country_id) {
	$ccode_id = array('273' => '', '1' => 'AF', '2' => 'AL', '3' => 'DZ', '4' => 'AS', '5' => 'AD', '6' => 'AO', '7' => 'AI', '8' => 'AQ', '9' => 'AG', '10' => 'AR', '11' => 'AM', '12' => 'AW', '13' => 'AU', '14' => 'AT', '15' => 'AZ', '16' => 'BS', '17' => 'BH', '18' => 'BD', '19' => 'BB', '20' => 'BY', '21' => 'BE', '22' => 'BZ', '23' => 'BJ', '24' => 'BM', '25' => 'BT', '26' => 'BO', '27' => 'BA', '28' => 'BW', '30' => 'BR', '274' => 'VG', '32' => 'BN', '33' => 'BG', '34' => 'BF', '275' => '', '35' => 'BI', '36' => 'KH', '37' => 'CM', '38' => 'CA', '39' => 'CV', '40' => 'KY', '41' => 'CF', '42' => 'TD', '43' => 'CL', '44' => 'CN', '45' => 'CX', '46' => 'CC', '47' => 'CO', '48' => 'CM', '49' => 'CD', '50' => 'CG', '51' => 'CK', '52' => 'CR', '54' => 'CI', '55' => 'HR', '272' => 'CU', '56' => 'CY', '57' => 'CZ', '58' => 'DK', '59' => 'DJ', '60' => 'DM', '61' => 'DO', '62' => 'TL', '63' => 'EC', '64' => 'EG', '65' => 'SV', '66' => 'GQ', '67' => 'ER', '68' => 'EE', '69' => 'ET', '70' => 'FK', '71' => 'FO', '72' => 'FJ', '73' => 'FI', '74' => 'FR', '276' => 'GF', '76' => 'PF', '78' => 'GA', '79' => 'GM', '253' => '', '80' => 'GE', '81' => 'DE', '82' => 'GH', '83' => 'GI', '84' => 'GR', '85' => 'GL', '86' => 'GD', '87' => 'GP', '88' => 'GU', '89' => 'GT', '90' => 'GG', '91' => 'GN', '92' => 'GW', '93' => 'GY', '94' => 'HT', '277' => 'VA', '96' => 'HN', '97' => 'HK', '98' => 'HU', '99' => 'IS', '100' => 'IN', '101' => 'ID', '102' => 'IR', '103' => 'IQ', '104' => 'IE', '105' => 'IL', '106' => 'IT', '107' => 'JM', '278' => 'SJ', '108' => 'JP', '279' => 'JE', '109' => 'JO', '110' => 'KZ', '111' => 'KE', '112' => 'KI', '114' => 'KP', '113' => 'KR', '115' => 'KW', '116' => 'KG', '117' => '', '118' => 'LV', '119' => 'LB', '120' => 'LS', '121' => 'LR', '122' => 'LY', '123' => 'LI', '124' => 'LT', '125' => 'LU', '126' => '', '127' => 'MK', '128' => 'MG', '129' => 'MW', '130' => 'MY', '131' => 'MV', '132' => 'ML', '133' => 'MT', '134' => 'MH', '135' => 'MQ', '136' => 'MR', '137' => 'MU', '138' => 'YT', '139' => 'MX', '140' => 'FM', '141' => 'MD', '142' => 'MC', '143' => 'MN', '260' => 'ME', '144' => 'MS', '145' => 'MA', '146' => 'MZ', '147' => 'MM', '148' => 'NA', '149' => 'NR', '150' => 'NP', '151' => 'AN', '152' => 'NL', '153' => 'NC', '154' => 'NZ', '155' => 'NI', '156' => 'NE', '157' => 'NG', '158' => 'NU', '159' => 'NF', '160' => 'MP', '161' => 'NO', '162' => 'OM', '163' => 'PK', '164' => 'PW', '165' => 'PA', '166' => 'PG', '167' => 'PY', '168' => 'PE', '169' => 'PH', '170' => 'PN', '171' => 'PL', '172' => 'PG', '173' => 'PR', '174' => 'QA', '175' => 'RE', '176' => 'RO', '177' => 'RU', '178' => 'RW', '179' => 'SH', '180' => 'KN', '181' => 'LC', '182' => 'PM', '183' => 'VC', '184' => 'WS', '185' => 'SM', '186' => 'ST', '187' => 'SA', '188' => 'SN', '259' => 'RS', '189' => 'SC', '190' => 'SL', '191' => 'SG', '192' => 'SK', '193' => 'SI', '194' => 'SB', '195' => 'SO', '196' => 'ZA', '198' => 'ES', '199' => 'LK', '200' => 'SD', '201' => 'SR', '202' => 'SJ', '203' => 'SZ', '204' => 'SE', '205' => 'CH', '206' => 'SY', '280' => '', '281' => 'TW', '208' => 'TJ', '209' => 'TZ', '210' => 'TH', '211' => 'TG', '212' => 'TK', '213' => 'TO', '214' => 'TT', '215' => 'TN', '216' => 'TR', '217' => 'TM', '218' => 'TC', '219' => 'TV', '220' => 'UG', '221' => 'UA', '222' => 'AE', '223' => 'GB', '224' => 'US', '226' => 'UY', '227' => 'UZ', '228' => 'VU', '229' => 'VA', '230' => 'VE', '231' => 'VN', '232' => 'VG', '233' => 'VI', '234' => 'WF', '252' => '', '282' => 'EH', '283' => 'WS', '235' => 'YE', '284' => '', '237' => 'ZM', '238' => 'ZW', );
	return $ccode_id{$country_id};
}

function match_country($country_id, $country_code) {
	require('bad_lists.php');
	if (preg_match('/^[A-Z]{2}$/', $country_code)) {
		if (country_id_to_code($country_id) != $country_code)
			return true;
		
	}
}

function country_blacklisted($bad_countries, $country_id, $country_code) {

# check country code from ip
	if ($country_code && in_array($country_code, $bad_countries)) {
		return true;
	}

# check country picked in registration
	if (in_array(country_id_to_code($country_id), $bad_countries)) {
		return true;
	}

}


function check_string($str, $filter, $debug = 0) {
	foreach ($filter as $check) {
		if (preg_match('/^rx!/', $check)) {
			$check = preg_replace('/^rx!/', '', $check);
			if (preg_match("/$check/si", $str)) {
				return true;
			}
		} else {
			if (stripos($str, $check) !== false) {
				return true;
			}
		}
	}
}

function get_ip_address() {
    $ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
    foreach ($ip_keys as $key) {
        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                // trim for safety measures
                $ip = trim($ip);
                // attempt to validate IP
                if (validate_ip($ip)) {
                    return $ip;
                }
            }
        }
    }
 
    return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;
}
 
function validate_ip($ip) {
    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) {
        return false;
    }
    return true;
}

?>